package com.cy.aircompressor;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;

import androidx.annotation.RequiresApi;

import com.drug.lib.util.DLog;

import java.util.Stack;



/**
 * 版权：星云飞救公司 版权所有
 *
 * @author zhouhequan
 * 版本：1.0
 * 文件名称 AppManager
 * 创建日期：2021/10/25 or 9:46
 * 描述：应用程序Activity管理类：用于Activity管理和应用程序退出
 */
@RequiresApi(api = Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public class AppManager implements Application.ActivityLifecycleCallbacks {

    private static final String TAG = "AppManager";

    private static Stack<Activity> activityStack;
    private static AppManager instance;

    private AppManager() {
    }

    /**
     * description: 单一实例
     *
     * @author: zhouhequan
     * @date: 2021/10/25 10:38
     * @return: AppManager
     */

    public static synchronized AppManager getAppManager() {
        if (instance == null) {
            synchronized (AppManager.class) {
                if (instance == null) {
                    instance = new AppManager();
                }
            }
        }
        return instance;
    }

    /**
     * description: 添加Activity到堆栈
     *
     * @author: zhouhequan
     * @date: 2021/10/25 10:38
     * @Param activity:
     * @return: void
     */

    public void addActivity(Activity activity) {
        if (activityStack == null) {
            activityStack = new Stack<>();
        }
        DLog.d("添加Activity到堆栈:" + activity.getClass().getName());
        activityStack.add(activity);

    }

    /**
     * description: 删除堆栈的Activity
     *
     * @author: zhouhequan
     * @date: 2021/10/25 10:39
     * @Param activity:
     * @return: void
     */

    public void removeActivity(Activity activity) {
        if (activityStack != null) {
            DLog.d("删除堆栈的Activity:" + activity.getClass().getName());
            activityStack.remove(activity);
        }
    }

    /**
     * description: 获取当前Activity（堆栈中最后一个压入的）
     *
     * @author: zhouhequan
     * @date: 2021/10/25 10:39
     * @return: android.app.Activity
     */

    public Activity getCurrentActivity() {
        Activity activity = activityStack.lastElement();
        DLog.d("获取堆栈中最后一个压入的:" + activity.getClass().getName());
        return activity;
    }

    /**
     * description: 结束当前Activity（堆栈中最后一个压入的）
     *
     * @author: zhouhequan
     * @date: 2021/10/25 10:39
     * @return: void
     */

    public void finishActivity() {
        Activity activity = activityStack.lastElement();
        finishActivity(activity);
    }

    /**
     * description: 结束指定的Activity
     *
     * @author: zhouhequan
     * @date: 2021/10/25 10:39
     * @Param activity:
     * @return: void
     */

    public void finishActivity(Activity activity) {
        if (activity != null) {
            activityStack.remove(activity);
            activity.finish();
            activity = null;
        }

    }

    /**
     * description: 结束指定类名的Activity
     *
     * @author: zhouhequan
     * @date: 2021/10/25 10:39
     * @Param cls:
     * @return: void
     */

    public void finishActivity(Class<?> cls) {
        for (Activity activity : activityStack) {
            if (activity.getClass().equals(cls)) {
                finishActivity(activity);
                break;
            }
        }
    }

    /**
     * description: 结束所有Activity
     *
     * @author: zhouhequan
     * @date: 2021/10/25 10:40
     * @return: void
     */

    public void finishAllActivity() {
        for (Activity activity : activityStack) {
            if (activity != null) {
                activity.finish();
            }
        }
        activityStack.clear();
    }

    /**
     * description: 退出栈中除指定的Activity外所有
     *
     * @author: zhouhequan
     * @date: 2021/10/25 10:40
     * @Param cls:
     * @return: void
     */

    public void popAllActivityExceptOne(Class cls) {

        for (Activity activity : activityStack) {
            if (activity != null && !activity.getClass().equals(cls)) {
                activityStack.remove(activity);
                activity.finish();
            }
        }
    }


    /**
     * description: 退出应用程序
     *
     * @author: zhouhequan
     * @date: 2021/10/25 10:40
     * @Param context:
     * @return: void
     */

    @SuppressLint("MissingPermission")
    public void appExit(Context context) {
        try {
            finishAllActivity();// 关闭所有Activity
            ActivityManager activityMgr = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            activityMgr.killBackgroundProcesses(context.getPackageName());
            System.exit(0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
        addActivity(activity);
    }

    @Override
    public void onActivityStarted(Activity activity) {

    }

    @Override
    public void onActivityResumed(Activity activity) {
    }

    @Override
    public void onActivityPaused(Activity activity) {

    }

    @Override
    public void onActivityStopped(Activity activity) {

    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

    }

    @Override
    public void onActivityDestroyed(Activity activity) {
        removeActivity(activity);
    }


    /**
     * description: 判断app是否在前台
     *
     * @author: zhouhequan
     * @date: 2021/10/25 10:40
     * @return: boolean
     */

    public boolean isOnForeground() {
        return activityStack != null && !activityStack.isEmpty();
    }
}